import { Behaviour, HitTestScript, TextRenderer, GameObject,getGameObjectById,core } from "../gameengine";
import { user } from "../user";
import {mouse}from "../monsters/mouse"
import{victory,defeated}from "../data"
import{RectCollider}from "../rect/RectCollider"
import { ColliderSystem } from "../rect/ColliderSystem";


export class Fightm extends Behaviour {
    startbattle=false;
    infight=true;
    bleed=mouse.att-user.def;
    battleresault=0;
    rst:GameObject;
    
    


    // private speedX = 0;
    // private speedY = 0;


    protected onStart() {
        


        

    }

    protected onUpdate(advancedTime: number) {

        const textRender1 = this.displayObject.children[0].getScript(TextRenderer);
        textRender1.text = user.hp.toString();
        const textRender2 = this.displayObject.children[1].getScript(TextRenderer);
        textRender2.text = user.def.toString();
        const textRender3 = this.displayObject.children[2].getScript(TextRenderer);
        textRender3.text = user.att.toString();
        const textRender5 = this.displayObject.children[3].getScript(TextRenderer);
        textRender5.text = mouse.hp.toString();
        const textRender6 = this.displayObject.children[4].getScript(TextRenderer);
        textRender6.text = mouse.att.toString();
        const textRender4 = this.displayObject.children[5].getScript(TextRenderer);
        textRender4.text = "开始对战!";

        
        const re=50;
        

        
        console.log(this.battleresault);
        console.log(this.infight);
        console.log(this.startbattle)
        
        if(!this.startbattle){
            window.addEventListener('click',(ev)=>{
                if(true){
                    
                        //this.rst.parent.children[2].removeAllChildren;
                        
                        // getGameObjectById('animals/5').getScript(RectCollider).type=0;
        
                        // getGameObjectById('animals/5').parent.removeChild(getGameObjectById('animals/5'));
                           
                       
                        
                        this.startbattle=true;
                          
                    
                    
                    ev.preventDefault();
                }
        
                })
            }
    
         
    
        if(this.startbattle){

            if(this.infight){
                if(mouse.att>user.def){
                    user.hp-=this.bleed;
                }else{
                    user.hp-=0;
                }
                    mouse.hp-=user.att;
                    if(user.hp>0 && mouse.hp>0){
                        this.infight=true;
                    }else{
                        this.infight=false;
                    }
                    if(!this.infight){
                        if(user.hp>=0){
                            this.battleresault=1;
                        }else{
                            this.battleresault=2;
                        }

                    }
                }
        }
        if(this.battleresault==1){
            this.displayObject.parent.removeChild(this.displayObject);
            this.displayObject = null;
            this.rst = core.createPrefab(victory);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);
                mouse.hp=re;
                user.gold +=mouse.gold;
               window.addEventListener('click',(ev)=>{
        if(true){
            if (this.rst) {
                //this.rst.parent.children[2].removeAllChildren;
                
                // getGameObjectById('animals/5').getScript(RectCollider).type=0;

                // getGameObjectById('animals/5').parent.removeChild(getGameObjectById('animals/5'));
                   
               
                
                  this.rst.parent.removeChild(this.rst)
                  this.rst = null;
                  //this.displayObject.removeChild{}
                  this.startbattle=false;
                  console.log('reset')
                  this.battleresault=0;
                  console.log('reset2')
                  mouse.switch=1;
                  
                  
            }
            
            ev.preventDefault();
        }

        })
            }

        
        if(this.battleresault==2){
            this.displayObject.parent.removeChild(this.displayObject)
            this.displayObject = null;
            this.rst = core.createPrefab(defeated);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);

        }

        

    }

}


